#ifndef __LCD_H__
#define __LCD_H__

#include "common.h"
#include "stm32f4xx_ll_dma.h"
#include "stm32f4xx_ll_spi.h"



#define LCD_COMM_TIMEOUT                   10000

#define LCD_SPI                            SPI3

#define LCD_SPI_CLK_ENABLE()               __HAL_RCC_SPI3_CLK_ENABLE()
#define LCD_SPI_CLK_DISABLE()              __HAL_RCC_SPI3_CLK_DISABLE()

#define LCD_GPIO_CLK_ENABLE() {	\
		__HAL_RCC_GPIOC_CLK_ENABLE();	\
		__HAL_RCC_GPIOA_CLK_ENABLE();	\
	};

#define LCD_GPIO_AF                   GPIO_AF6_SPI3

#define LCD_SCK_PORT				  GPIOC
#define LCD_SCK_PIN					  GPIO_PIN_10

#define LCD_MISO_PORT				  GPIOC
#define LCD_MISO_PIN			      GPIO_PIN_11

#define LCD_MOSI_PORT				  GPIOC
#define LCD_MOSI_PIN			      GPIO_PIN_12

#define LCD_CS_PORT					  GPIOC
#define LCD_CS_PIN					  GPIO_PIN_0

#define LCD_DC_PORT					  GPIOC
#define LCD_DC_PIN					  GPIO_PIN_1

#define LCD_RESET_PORT			      GPIOA
#define LCD_RESET_PIN				  GPIO_PIN_2

#define LCD_CS_0()					  LCD_CS_PORT->BSRR = ((uint32_t)LCD_CS_PIN << 16U) 
#define LCD_CS_1()					  LCD_CS_PORT->BSRR = LCD_CS_PIN

#define LCD_DC_0()					  LCD_DC_PORT->BSRR = ((uint32_t)LCD_DC_PIN << 16U) 
#define LCD_DC_1()					  LCD_DC_PORT->BSRR = LCD_DC_PIN

#define LCD_RESET_0()				  LCD_RESET_PORT->BSRR = ((uint32_t)LCD_RESET_PIN << 16U) 
#define LCD_RESET_1()				  LCD_RESET_PORT->BSRR = LCD_RESET_PIN

#define LCD_BACKLIGHT_PORT			  GPIOA
#define LCD_BACKLIGHT_PIN			  GPIO_PIN_1
#define LCD_BACKLIGHT_TIMER           TIM2
#define LCD_BACKLIGHT_CHANNEL         TIM_CHANNEL_2
#define LCD_BACKLIGHT_PRESCALE        4
#define LCD_BACKLIGHT_PERIOD          1000

#define LCD_BACKLIGHT_GPIO_CLK_ENABLE() {	\
		__HAL_RCC_GPIOA_CLK_ENABLE();	\
	};

#define LCD_BACKLIGHT_TIM_CLK_ENABLE() {	\
		__HAL_RCC_TIM2_CLK_ENABLE();	\
	};


#define LCD_WIDTH                     240
#define LCD_HEIGHT                    135

typedef enum
{
    LCD_COLOR_WHITE = 0xFFFF,
    LCD_COLOR_BLACK = 0x0000,
    LCD_COLOR_BLUE = 0x001F,
    LCD_COLOR_BLUE2 = 0x051F,
    LCD_COLOR_RED = 0xF800,
    LCD_COLOR_MAGENTA = 0xF81F,
    LCD_COLOR_GREEN = 0x07E0,
    LCD_COLOR_CYAN = 0x7FFF,
    LCD_COLOR_YELLOW = 0xFFE0
}LCD_COLOR_E;

void LcdSpiInit(void);
void LcdSpiDeInit(void);
void LcdSpiSendData(unsigned char _ucData);
void LcdSpiSendData16(unsigned short _usData);
void LcdWriteCmd(unsigned char _ucCmd);
void LcdWriteData(unsigned char _ucData);
void LcdWriteData16(uint16 _usData);
void LcdBackLightInit(void);
void LcdSetBackLight(uint32 _uiValue);
void LcdInit(void);
void LcdDeInit(void);
void LcdStartWrite(void);
void LcdEndWrite(void);
bool LcdSetAddress(uint16_t _usX, uint16_t _usY, uint16_t _usWidth, uint16_t _usHeight);
void LcdDrawPixels(uint16_t *_pusColor, uint16_t _usSize);
void LcdFillScreen(uint16 _usColor);

#endif

